package check;

import java.math.BigInteger;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//列信息记录

//                    TABLE_CATALOG	varchar	512	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    TABLE_SCHEMA	varchar	64	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    TABLE_NAME	varchar	64	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    COLUMN_NAME	varchar	64	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0


//                    ORDINAL_POSITION	bigint	21	0	0	0	-1	0	0	0	0					0	0
//                    COLUMN_DEFAULT	longtext	0	0	-1	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    IS_NULLABLE	varchar	3	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    DATA_TYPE	varchar	64	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    CHARACTER_MAXIMUM_LENGTH	bigint	21	0	-1	0	-1	0	0		0					0	0
//                    CHARACTER_OCTET_LENGTH	bigint	21	0	-1	0	-1	0	0		0					0	0
//                    NUMERIC_PRECISION	bigint	21	0	-1	0	-1	0	0		0					0	0
//                    NUMERIC_SCALE	bigint	21	0	-1	0	-1	0	0		0					0	0
//                    DATETIME_PRECISION	bigint	21	0	-1	0	-1	0	0		0					0	0
//                    CHARACTER_SET_NAME	varchar	32	0	-1	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    COLLATION_NAME	varchar	32	0	-1	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    COLUMN_TYPE	longtext	0	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    COLUMN_KEY	varchar	3	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    EXTRA	varchar	30	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    PRIVILEGES	varchar	80	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    COLUMN_COMMENT	varchar	1024	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    IS_GENERATED	varchar	6	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    GENERATION_EXPRESSION	longtext	0	0	-1	0	0	0	0		0		utf8	utf8_general_ci		0	0
public class CheckTable {
    public static final String column_name = "column_name";

    public static void main(String[] args) throws Exception {
        Connection connection = JdbcUtil.getConnection();
        Map<String, Map<String, Object>> mapMap = new HashMap<String, Map<String, Object>>();
        List<Map<String, Object>> mapListFor$model_table = JdbcUtil.get(connection, "select t.* from INFORMATION_SCHEMA.Columns  t where t.table_name='model_table'", null);
        JdbcUtil.close(null, null, connection);
        /**
         * 依次按照列名进行处理
         */
        List<String> columnNameList = new ArrayList<String>();
        for (Map<String, Object> map :
                mapListFor$model_table) {
            for (String key :
                    map.keySet()) {
                System.out.println("key:" + key + "," + map.get(key));
            }
            String columnName = (String) map.get(column_name);
            columnNameList.add(columnName);
            mapMap.put(columnName, map);
        }


        //table_index
        connection = JdbcUtil.getConnection();
        List<Map<String, Object>> mapListFor$useThisModel = JdbcUtil.get(connection, "select t.* from INFORMATION_SCHEMA.Columns  t where t.table_name='table_index_copy'", null);
        //第1校验基础字段顺序必须是一致的
        //第2校验基础字段类型一致 长度一致 默认值一致 如果含有主键则判断主键是否存在,如果没有主键则不能有主键
        System.out.println("===========================================1");
        int index = 0;
        for (Map<String, Object> map :
                mapListFor$useThisModel) {
            String columnName = (String) map.get(column_name);
            String columnNameWith = columnNameList.get(index);
            if (!columnName.equals(columnNameWith)) {
                String msg = "列不匹配;columnName:" + columnName + "," + columnNameWith + ":" + columnNameWith;
                throw new RuntimeException(msg);
            }
            index++;
            if (index == columnNameList.size()) {
                break;
            }
        }
        //
        System.out.println("===========================================2");
        for (Map<String, Object> map :
                mapListFor$useThisModel) {
            //判断是否在模版列中

            String columnName = (String) map.get(column_name);

            if (!mapMap.containsKey(columnName)) {
                break;
            }
            System.out.println("columnName:" + columnName);
            //主键校验
            //先按照类型进行校验
            //再校验默认值
            //长度检验
            System.out.println("%%%columnName%%%:" + columnName);
            Map<String, Object> mapIn$model_table = mapMap.get(columnName);
            //首先对比那些需要进行校验的
            for (String key : mapIn$model_table.keySet()) {
                //首先检查是否在对方
                if (!map.containsKey(key)) {
                    String msg = "字段:" + key + "当前表 ";
                    throw new RuntimeException(msg);
                }

                System.out.println(mapIn$model_table);
                Object object = map.get(key);
                Object objectIn$model_table = mapIn$model_table.get(key);

                if (object == null && objectIn$model_table == null) {
                    //
                } else if ((object == null && objectIn$model_table != null) || (object != null && objectIn$model_table == null)) {
                    //直接存在不一致
                    String msg = "字段:" + columnName + "ordinal_position不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                    throw new RuntimeException(msg);
                } else if (object != null && objectIn$model_table != null) {
                    //xx
                    System.out.println("key:" + object + "," + objectIn$model_table);


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                    table_catalog	varchar	512	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    table_schema	varchar	64	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    table_name	varchar	64	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0
//                    column_name	varchar	64	0	0	0	0	0	0		0		utf8	utf8_general_ci		0	0

//具体列信息
//                    ordinal_position	bigint        第几列
//                    column_default	longtext      默认值
//                    is_nullable	varchar           是否为空
//                    data_type	varchar               数据类型
//                    character_maximum_length	bigint字符最大长度
//                    character_octet_length	bigint
//                    numeric_precision	bigint        数字精度
//                    numeric_scale	bigint            数字小数位
//                    datetime_precision	bigint    日期精度
//                    character_set_name	varchar   字符名字
//                    collation_name	varchar       collation
//                    column_type	longtext          列类型
//                    column_key	varchar           列key
//                    extra	varchar	30                extra
//                    privileges	varchar           权限
//                    column_comment	varchar       列注释
//                    is_generated	varchar           是否自增
//                    generation_expression	longtext


//                    ordinal_position 	bigint
                    if ("ordinal_position".equals(key)) {
                        BigInteger ordinal_position = (BigInteger) object;
                        BigInteger ordinal_positionIn$model_table = (BigInteger) objectIn$model_table;
                        if (ordinal_position.compareTo(ordinal_positionIn$model_table) != 0) {
                            String msg = "字段:" + key + "ordinal_position不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    column_default	longtext
                    if ("column_default".equals(key)) {
                        String column_default = (String) object;
                        String column_defaultIn$model_table = (String) objectIn$model_table;
                        if (!column_default.equals(column_defaultIn$model_table)) {
                            String msg = "字段:" + key + "column_default不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    is_nullable	varchar
                    if ("is_nullable".equals(key)) {
                        String is_nullable = (String) object;
                        String is_nullableIn$model_table = (String) objectIn$model_table;
                        if (!is_nullable.equals(is_nullableIn$model_table)) {
                            String msg = "字段:" + key + "is_nullable不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    data_type	varchar
                    if ("data_type".equals(key)) {
                        String data_type = (String) object;
                        String data_typeIn$model_table = (String) objectIn$model_table;
                        if (!data_type.equals(data_typeIn$model_table)) {
                            String msg = "字段:" + key + "data_type不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    character_maximum_length	bigint
                    if ("character_maximum_length".equals(key)) {
                        BigInteger character_maximum_length = (BigInteger) object;
                        BigInteger character_maximum_lengthIn$model_table = (BigInteger) objectIn$model_table;
                        if (character_maximum_length.compareTo(character_maximum_lengthIn$model_table) != 0) {
                            String msg = "字段:" + key + "character_maximum_length不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
                    //CHARACTER_OCTET_LENGTH应与CHARACTER_MAXIMUM_LENGTH相同，但多字节字符集除外。
//                    character_octet_length	bigint
                    if ("character_octet_length".equals(key)) {
                        //not check  character_maximum_length: character_octet_length=1:4
                    }
//                    numeric_precision	bigint
                    if ("numeric_precision".equals(key)) {//num-1
                        BigInteger numeric_precision = (BigInteger) object;
                        BigInteger numeric_precisionIn$model_table = (BigInteger) objectIn$model_table;
                        if (numeric_precision.compareTo(numeric_precisionIn$model_table) != 0) {
                            String msg = "字段:" + key + "numeric_precision不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    numeric_scale	bigint
                    if ("numeric_scale".equals(key)) {
                        BigInteger numeric_scale = (BigInteger) object;
                        BigInteger numeric_scaleIn$model_table = (BigInteger) objectIn$model_table;
                        if (numeric_scale.compareTo(numeric_scaleIn$model_table) != 0) {
                            String msg = "字段:" + key + "numeric_scale不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    datetime_precision	bigint
                    if ("datetime_precision".equals(key)) {
                        BigInteger datetime_precision = (BigInteger) object;
                        BigInteger datetime_precisionIn$model_table = (BigInteger) objectIn$model_table;
                        if (datetime_precision.compareTo(datetime_precisionIn$model_table) != 0) {
                            String msg = "字段:" + key + "datetime_precision不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
                    //字符编码都是一个东西,统一
//                    character_set_name	varchar
                    if ("character_set_name".equals(key)) {
                        //not check
                    }
//                    collation_name	varchar
                    if ("collation_name".equals(key)) {
                        //not check
                    }
//                    column_type	longtext
                    if ("column_type".equals(key)) {
                        String column_type = (String) object;
                        String column_typeIn$model_table = (String) objectIn$model_table;
                        if (!column_type.equals(column_typeIn$model_table)) {
                            String msg = "字段:" + key + "column_type不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    column_key	varchar
                    if ("column_key".equals(key)) {//PRI
                        String column_key = (String) object;
                        String column_keyIn$model_table = (String) objectIn$model_table;
                        if (!column_key.equals(column_keyIn$model_table)) {
                            String msg = "字段:" + key + "column_key不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    extra	varchar	30
                    if ("extra".equals(key)) {//auto_increment or on update current_timestamp()
                        String extra = (String) object;
                        String extraIn$model_table = (String) objectIn$model_table;
                        if (!extra.equals(extraIn$model_table)) {
                            String msg = "字段:" + key + "extra不一致,模版表是:" + objectIn$model_table + ",当前表是:" + object;
                            throw new RuntimeException(msg);
                        }
                    }
//                    privileges	varchar
                    if ("privileges".equals(key)) {
                        //not check
                    }
//                    column_comment	varchar
                    if ("column_comment".equals(key)) {
                        //not check
                    }
//                    is_generated	varchar
                    if ("is_generated".equals(key)) {
                        //not check
                    }
//                    generation_expression	longtext
                    if ("generation_expression".equals(key)) {
                        //not check
                    }


                }

            }


        }
        System.out.println("校验成功");

    }

}